// WriteEeprom.cpp : implementation file
//

#include "stdafx.h"
#include "ubox_recorder_test.h"
#include "WriteEeprom.h"
#include "ubox_recorder_testDlg.h"


// WriteEeprom dialog

IMPLEMENT_DYNAMIC(WriteEeprom, CDialog)

WriteEeprom::WriteEeprom(CWnd* pParent /*=NULL*/)
	: CDialog(WriteEeprom::IDD, pParent)
	, data(_T(""))
{
    b_write = false;
	b_read = false;
}

WriteEeprom::~WriteEeprom()
{
}

void WriteEeprom::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT1, data);
}


BEGIN_MESSAGE_MAP(WriteEeprom, CDialog)
	ON_BN_CLICKED(IDC_WRITE, &WriteEeprom::OnBnClickedWrite)
	ON_BN_CLICKED(IDC_READ, &WriteEeprom::OnBnClickedRead)
END_MESSAGE_MAP()


// WriteEeprom message handlers

ULONG StringHexToByteArray(CString Str, PBYTE pData)
{
	int Len;
    BYTE result = 0;
    ULONG RET = 0;
    CString temp1, temp2;
    CHAR t;

    Str.MakeUpper();
    Str.TrimLeft();
    Str.TrimRight();
    Len = Str.GetLength();

    int lastPos = 0, Pos = Str.Find(' ');

    while(Pos != -1)
    {
        temp1 = Str.Mid(lastPos, Pos - lastPos);
        if(temp1 == " ")
        {
            lastPos ++;
            Pos = Str.Find(' ', lastPos+1);
            continue;
        }

        result = 0;
        for(int i = 0; i < temp1.GetLength(); i++)
        {
            temp2 = temp1.Mid(i,1);
            if (temp2.FindOneOf("0123456789") != -1)
            {
                t = temp2.GetAt(0);
                t = t - '0';
                result = result * 16 + t;
            }
            if (temp2.FindOneOf("ABCDEF") != -1)
            {
                t = temp2.GetAt(0);
                t = t - 'A' + 10;
                result = result * 16 + t;
            }
        }
        *pData = result;
        pData++;
        RET++;
        lastPos = Pos;
        Pos = Str.Find(' ', lastPos+1);
    }

    if (Str.GetLength() > 0 && Len != lastPos)
    {
        temp1 = Str.Mid(lastPos, Len - lastPos);
        result = 0;
        for(int i = 0; i < temp1.GetLength(); i++)
        {
            temp2 = temp1.Mid(i,1);
            if (temp2.FindOneOf("0123456789") != -1)
            {
                t = temp2.GetAt(0);
                t = t - '0';
                result = result * 16 + t;
            }
            if (temp2.FindOneOf("ABCDEF") != -1)
            {
                t = temp2.GetAt(0);
                t = t - 'A' + 10;
                result = result * 16 + t;
            }
        }
        *pData = result;
        pData++;
        RET++;
    }

    return RET;
}

void WriteEeprom::OnBnClickedWrite()
{
	// TODO: Add your control notification handler code here
	int ubox_handle = ((CUbox_recorder_testDlg*)AfxGetApp()->m_pMainWnd)->m_iClickItemID;
	CUBoxLine* pLine = NULL;
	int res;
	//CListBox dis;
	//dis = ((CUbox_recorder_testDlg*)AfxGetApp()->m_pMainWnd)->m_dis;
	pLine = ((CUbox_recorder_testDlg*)AfxGetApp()->m_pMainWnd)->get_line_by_ID(ubox_handle);
	if(pLine)
	{
       UBOX_HANDLE hdl = pLine->line_handel();
		unsigned char wdata[8] = {170,2,3,4,98,6,7,255};    //1->0x1  255->0xFF  data[i]<256;

		  UpdateData(TRUE);

		  //pLine->EepromDlg.b_write = false;

          StringHexToByteArray(data,wdata);
         /* if(ubox_send_dtmf(hdl,pLine->SendDtmfDlg.m_phonenum) !=0)
			  m_dis.InsertString(m_dis.GetCount(),"dial num failed"); */

		  char buff[100];

			sprintf(buff,"0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x",wdata[0],wdata[1],wdata[2],wdata[3],wdata[4],wdata[5],wdata[6],wdata[7]);
			((CUbox_recorder_testDlg*)AfxGetApp()->m_pMainWnd)->m_dis.InsertString(((CUbox_recorder_testDlg*)AfxGetApp()->m_pMainWnd)->m_dis.GetCount(),buff);

		  res = ubox_write_eeprom(hdl,0,wdata,8);   //data [0,255]
		  if(res !=0)
			  ((CUbox_recorder_testDlg*)AfxGetApp()->m_pMainWnd)->m_dis.InsertString(((CUbox_recorder_testDlg*)AfxGetApp()->m_pMainWnd)->m_dis.GetCount(),"read eepromex data failed");
		  else
		  {
			  ((CUbox_recorder_testDlg*)AfxGetApp()->m_pMainWnd)->m_dis.InsertString(((CUbox_recorder_testDlg*)AfxGetApp()->m_pMainWnd)->m_dis.GetCount(),"write data ok");

		  }
	}
	b_write = true;
}

void WriteEeprom::OnBnClickedRead()
{
	// TODO: Add your control notification handler code here
	b_read = true;
	int ubox_handle = ((CUbox_recorder_testDlg*)AfxGetApp()->m_pMainWnd)->m_iClickItemID;
	CUBoxLine* pLine = NULL;
	int res;
	//CListBox dis;
	//dis = ((CUbox_recorder_testDlg*)AfxGetApp()->m_pMainWnd)->m_dis;
	pLine = ((CUbox_recorder_testDlg*)AfxGetApp()->m_pMainWnd)->get_line_by_ID(ubox_handle);
	if(pLine)
	{
		UBOX_HANDLE hdl = pLine->line_handel();
		unsigned char rdata[8];
		res = ubox_read_eepromEx(hdl,0,rdata,8);
		if(res !=0)
			((CUbox_recorder_testDlg*)AfxGetApp()->m_pMainWnd)->m_dis.InsertString(((CUbox_recorder_testDlg*)AfxGetApp()->m_pMainWnd)->m_dis.GetCount(),"read eeprom data failed");
		else
		{

			//m_dis.InsertString(m_dis.GetCount(),"Read eepromdata from addr 0-7:");
			char buff[100];

			sprintf(buff,"0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x",rdata[0],rdata[1],rdata[2],rdata[3],rdata[4],rdata[5],rdata[6],rdata[7]);
			((CUbox_recorder_testDlg*)AfxGetApp()->m_pMainWnd)->m_dis.InsertString(((CUbox_recorder_testDlg*)AfxGetApp()->m_pMainWnd)->m_dis.GetCount(),buff);

		}
	}
}
